package nave.model;

import java.util.Random;

import nave.Constants;
import nave.Point;

public class Player extends Spaceship {

	/*
	 * Attributes
	 */
	private int lives;
	private static int score;

	/*
	 * Constructor
	 */
	public Player(int lives, int numberOfBullets ) {

		super(Constants.ID_PLAYER, numberOfBullets);

		score = 0;

		this.lives = lives;
	}

	/*
	 * Getters and Setters
	 */
	public int getLives() {
		return this.lives;
	}
	public int getScore() {
		return score;
	}
	public static void score(int points) {
		score += points;
	}
	
	/*
	 * Public methods
	 */
	@Override
	public void placeOnStage() {

		Random randomGenerator = new Random();

		int rowsAux = 0,
			colsAux = 0;

		do
		{
			rowsAux = randomGenerator.nextInt( Stage.getRows() );
			colsAux = randomGenerator.nextInt( Stage.getCols() );
		} while ( Stage.getPositionOnStage( rowsAux, colsAux ) != Constants.EMPTY );

		position.setLocation(colsAux, rowsAux);

		Stage.setPositionOnStage( position, id );

		setStatus( STATUS_INVENCIBLE );
		new Thread() {
			public void run() {

				int sleep = Constants.INVENCIBILITY_BORN_TIME * 1000;

				try {
					Thread.sleep(sleep);
				} catch (InterruptedException e) {
					setStatus( STATUS_NORMAL );
				}
				setStatus( STATUS_NORMAL );
			}
		}.start();
	}
	
	@Override
	public void explode() {
		score( Constants.POINTS_PLAYER_DIED );
		this.lives--;

		finishMove();

		isExploding = true;
		Stage.setPositionOnStage(position, Constants.EMPTY);

		if ( this.lives > 0 ) replace();
	}

	public void replace() {
		// TODO Auto-generated method stub

		placeOnStage();

		deltaMove = new Point(0, 0);
		nextDirection = 0;
		
		drawPoint = new Point(position.x * Constants.TILE_WIDTH,
				position.y * Constants.TILE_HEIGHT);
	}

	/*
	 * Private methods
	 */
	@Override
	protected void extra() {}

}
